package com.hspedu.streamtest;

import com.hspedu.lambda2.Employee;
import com.hspedu.lambda2.EmployeeData;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/30
 * @time 16:49
 */
public class StreamAPITest2 {
    //第一种：匹配和查找
    @Test
    public void test1(){
        List<Employee> employees = EmployeeData.getEmployees();
        boolean isAllMatch = employees.stream().allMatch(employee -> employee.getAge() > 18);
        System.out.println("isAllMatch = " + isAllMatch);

        boolean allMatch = employees.stream().allMatch(employee -> employee.getSalary() > 10000);
        System.out.println(allMatch);

        Optional<Employee> first = employees.stream().findFirst();
        System.out.println(first);

        Optional<Employee> any = employees.parallelStream().findAny();
        System.out.println(any);
    }


    @Test
    public void test2(){
        List<Employee> employees = EmployeeData.getEmployees();
        long count = employees.stream().filter(employee -> employee.getSalary() > 5000).count();
        System.out.println(count);

        Optional<Employee> max = employees.stream().max(Comparator.comparingDouble(Employee::getSalary));
        System.out.println(max);
    }

    /**
     * 规约
     */
    @Test
    public void test3(){
        List<Integer> nums = Arrays.asList(1, 2, 34, 5, 6);
        Integer sum = nums.stream().reduce(0, Integer::sum);
        System.out.println(sum);

        List<Employee> employees = EmployeeData.getEmployees();
        Optional<Double> sumMoney = employees.stream().map(employee -> employee.getSalary()).reduce(Double::sum);
        System.out.println(sumMoney);
    }
}
